MC-10 Operation and Language Reference Manual Chapter 12

12/READING DATA So far, you've learned that there are two ways to enter numbers into your programs: By using assignment of variables to constants (LET a=5). By using the INPUT statement (INPUT A).
DATA item, item, item... item is either a string or numeric constant. Expressions are not allowed. If item includes colons, commas, or leading blanks, you must enclose these values in quotes. Every item but the last must be followed by a comma.
READ variable, variable, variable... variable is either a string or numeric variable. Every variable but the last must be followed by a comma.
DATA lines can only be read by the READ statement. There must be the same number of variables following READ that there are items following DATA. If there are more READ variables than DATA items, an Out of Data Error message will appear. DATA lines can be placed anywhere in the program. For convenience, we suggest you place your DATA statements either at the first or at the end of the program so they can be easily found. Try this short example: 10 DATA 1,2,3,4,5 20 FOR N = 1 TO 5 30 READ A 40 PRINT A; 50 NEXT N 60 END When you RUN the program, the Display will look like this: 1 2 3 4 5 OK Notice that the DATA items (line 10) are outside the loop. Delete line 10 (type 10 [ENTER]) and retype it as line 25. 25 DATA 1,2,3,4,5 Still no difference. What happens is that Micro Color BASIC loops through N five times, reading the variable A (which contains the data) each time. Each time the program loops, the data is printed-but each time, the value of A has been incremented. It's important to note that each piece of data in a DATA line can be read only once each time the program is run. The next time a READ statement requests a piece of data, it will read the next piece of data in the DATA line, or, if that line is all used up, go on to the next DATA line and start reading it. Is there a way around this? Yes, just use... The RESTORE Statement Whenever Micro Color BASIC comes across a RESTORE, all DATA lines are restored to their original "unread" condition. This includes those which have been read and those that haven't.
RESTORE
To see how useful RESTORE can be, change the above program back to its original state: 10 DATA 1,2,3,4,5 20 FOR N = 1 TO 5 30 READ A 40 PRINT A; 50 NEXT N 60 END and add this line: 57 GOTO 10 From everything you've learned so far, you might think the program would just re-cycle through an "endless" loop. RUN the program just to see. Did you get an Out of Data Error? Remember we said that when data has been read once in a program, it can't be read again unless you include RESTORE. Add this line: 55 RESTORE and then RUN the program. Now you have an endless loop. (Press [BREAK] to get out of it.) Reading String Variables DATA, READ, and RESTORE can be used with text as well as with numeric values. To use text values, include actual words in your DATA statements, and have the program READ string values (a variable with the suffix $). For instance: 10 DATA APPLES, ORANGES, PEARS 20 FOR N = 1 TO 3 30 READ A$ 40 PRINT A$ 50 NEXT N 60 END Avoiding Out of Data Errors If you're writing complicated programs with a number of DATA items, we suggest that the last DATA item be "-1" or a similar check value. Then, elsewhere in the program, compare that value to your variables. See the following program for an example. Using DATA and READ Effectively DATA and READ are much more powerful than the examples we've used. The following program, which computes the average number of goldfish in the typical American household, illustrates this. Note two things about this program: It includes a "check" at the end of the DATA statement which tells Micro Color BASIC that the end of the data has been reached. This is accomplished by making the last item -1, then checking for -1 in a program line. It does not compute the number of individual goldfish in each household. You must obtain and type in these values. 10 DATA 3,17,18,11,50,12,18,10,2,23 20 DATA 33,81,77,66,32,11,19,18,33,1 30 DATA 25,16,14,13,33,-1 100 TTL = 0 :REM TTL = TOTAL NUMBER 110 READ GF :REM GF = GOLDFISH 120 IF GF = -1 THEN 150 :REM CHECK FOR END OF DATA 130 TTL = TTL + GF :REM INCREMENT TOTAL 140 GOTO 110 :REM READ SOME MORE 150 AVG = TTL/25 :REM AVG = AVERAGE/ 25 = NUMBER OF FAMILIES SURVEYED 160 PRINT "THE AVERAGE FAMILY ONTAINS"; AVG; "GOLDFISH." 170 END When you RUN the program, you find out that, of the 25 families surveyed, the average number of goldfish per household was 25.44. There Has To Be An Easier Way... Both READ and RESTORE can be entered using the Command Function key-combinations. (DATA must be typed in.) To enter READ, press the key-combination of: [CONTROL][T] To enter RESTORE, press the key-combination of: [CONTROL][Y]

Comments should be directed to the following email address:
dsbrain@neosplice.com